package org.dbwebx.flex.delegate.impl {
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.ResultEvent;
	
	import org.dbwebx.domain.SourceNode;
	import org.dbwebx.flex.common.event.SourceNodeEvent;
	import org.dbwebx.flex.delegate.impl.base.SourceNodeServiceBase;
	import org.swizframework.Swiz;

	[Bindable]
    public class SourceNodeService extends SourceNodeServiceBase {

		public function SourceNodeService()
		{
			super();
		}
		
		/**
		 * Dispatches event that source node was found. 
		 * @param result the remote invocation result
		 */
		override protected function findSuccessHandler(result:ResultEvent, ... args:Array):void
		{
			if(null != result){
				var sourceNode:SourceNode = SourceNode(result.result);
				var evt:SourceNodeEvent = new SourceNodeEvent(SourceNodeEvent.SN_FOUND_EVT);
				evt.sourceNode = sourceNode;
				Swiz.dispatchEvent(evt);
			}
		}
		
		/**
		 * Dispatches event that source node was loaded. 
		 * @param result the remote invocation result
		 */
		override protected function loadSuccessHandler(result:ResultEvent, ... args:Array):void
		{
			if(null != result){
				var sourceNode:SourceNode = SourceNode(result.result);
				var evt:SourceNodeEvent = null;
				if(null != args && args[0] is String){
					evt = new SourceNodeEvent(args[0]);
				}
				if(null==evt){
					evt = new SourceNodeEvent(SourceNodeEvent.SN_LOADED_EVT);
				}
				evt.sourceNode = sourceNode;
				Swiz.dispatchEvent(evt);
			}
		}
		
		override protected function listSuccessHandler(result:ResultEvent, ... args:Array):void
		{
			if(null != result){
				var sourceNodes:ArrayCollection = ArrayCollection(result.result);
				var evt:SourceNodeEvent = null;
				if(null != args && args.length > 0){
					evt = new SourceNodeEvent(args[0]);
				}
				if(null==evt){
					evt = new SourceNodeEvent(SourceNodeEvent.SN_LIST_LOADED_EVT);
				}
				evt.sourceNodes = sourceNodes;
				Swiz.dispatchEvent(evt);
			}
		}
		
    }
}